﻿/* Custom USB HID Communication Devices
 * By Omar Zabala.
 *
 * ***MOUSE NOT FUNCTIONAL***
 * The mouse class is not yet functional.
 * 
 * Licensed under the Apache 2.0 open source license
 */

using System;
using System.Threading;
using Microsoft.SPOT;  
using Microsoft.SPOT.Hardware;
using Microsoft.SPOT.Hardware.UsbClient;
using SecretLabs.NETMF.Hardware;
using SecretLabs.NETMF.Hardware.Netduino;

namespace Netduino_USB_HID_Library
{
    public class Program
    {
        static OutputPort led = new OutputPort(Pins.ONBOARD_LED, false);
        static bool enable = false;

        public static void Main()
        {
            InterruptPort button = new InterruptPort(Pins.ONBOARD_SW1, false, Port.ResistorMode.Disabled, Port.InterruptMode.InterruptEdgeBoth);
            button.OnInterrupt += new NativeEventHandler(button_OnInterrupt);

            string setupResult = Keyboard.SetUp();

            if (setupResult != "Success")
                return;
            
            // Give some time for user to connect USB before things happen.
            led.Write(true);
            Thread.Sleep(2000);
            led.Write(false);
            Thread.Sleep(2000);


            while (true)
            {
                if (enable)
                {
                    // For all supported chars, check the list of supported chars in the keyboard class.

                    Keyboard.Send("abcdefghijklmnopqrstuvwxyz \n");
                    Keyboard.Send("ABCDEFGHIJKLMNOPQRSTUVWXYZ \n");
                    Keyboard.Send("1234567890 \n");
                    Keyboard.Send("- = [ { ] } \\ | , > . < / ? ! @ # $ % ^ & * ( ) \n");
                }
                Thread.Sleep(500);
            }
        }

        private static void button_OnInterrupt(uint data1, uint data2, DateTime time)
        {
            if (data2 == 0)
                enable = !enable;

            led.Write(enable);
        }
    }
}